<?php

declare(strict_types=1);

use app\model\NotifyTemplate;
use think\migration\Migrator;

class SeedNotificationTemplates extends Migrator
{
    public function up(): void
    {
        $templates = $this->templates();

        NotifyTemplate::whereIn('type', array_column($templates, 'type'))->delete();

        $model = new NotifyTemplate();
        $model->saveAll($templates);
    }

    public function down(): void
    {
        NotifyTemplate::whereIn('type', array_column($this->templates(), 'type'))->delete();
    }

    private function templates(): array
    {
        $json = static fn(array $value) => json_encode($value, JSON_UNESCAPED_UNICODE);

        return [
            [
                'name' => '系统消息',
                'title' => '{title}',
                'content' => '{message}',
                'type' => 'sys_message',
                'category' => 'system',
                'channels' => $json([
                    'websocket' => [
                        'enabled' => true,
                        'route' => '{route}',
                    ],
                    'wechat' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                    'sms' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                ]),
            ],
            [
                'name' => '现货核实申请',
                'title' => '现货核实申请',
                'content' => '您有一个新的现货核实申请#{verify_no}，请核实',
                'type' => 'spotcheck',
                'category' => 'trade',
                'channels' => $json([
                    'websocket' => [
                        'enabled' => true,
                        'route' => '/seller/trade/spotcheck?verify_no={verify_no}',
                    ],
                    'wechat' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                    'sms' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                ]),
            ],
            [
                'name' => '现货核实回复',
                'title' => '现货核实回复',
                'content' => '您的现货核实申请#{verify_no}已回复，请查看',
                'type' => 'spotcheck_reply',
                'category' => 'trade',
                'channels' => $json([
                    'websocket' => [
                        'enabled' => true,
                        'route' => '/cart?type=spotcheck&verify_no={verify_no}',
                    ],
                    'wechat' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                    'sms' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                ]),
            ],
            [
                'name' => '期货询价申请',
                'title' => '期货询价申请',
                'content' => '您有一个新的期货询价申请#{verify_no}，请回复',
                'type' => 'futures',
                'category' => 'trade',
                'channels' => $json([
                    'websocket' => [
                        'enabled' => true,
                        'route' => '/seller/trade/futures?verify_no={verify_no}',
                    ],
                    'wechat' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                    'sms' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                ]),
            ],
            [
                'name' => '期货询价回复',
                'title' => '期货询价回复',
                'content' => '您的期货询价申请#{verify_no}已回复，请查看',
                'type' => 'futures_reply',
                'category' => 'trade',
                'channels' => $json([
                    'websocket' => [
                        'enabled' => true,
                        'route' => '/cart?type=futures&verify_no={verify_no}',
                    ],
                    'wechat' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                    'sms' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                ]),
            ],
            [
                'name' => '订单支付',
                'title' => '订单支付通知',
                'content' => '订单#{order_no}已支付成功，金额：¥{amount}，请安排发货。',
                'type' => 'order_paid',
                'category' => 'order',
                'channels' => $json([
                    'websocket' => [
                        'enabled' => true,
                        'route' => '/system/order/list?order_no={order_no}',
                    ],
                    'wechat' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                    'sms' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                ]),
            ],
            [
                'name' => '订单发货',
                'title' => '订单发货通知',
                'content' => '订单#{order_no}需要发货，请及时处理。',
                'type' => 'order_ship',
                'category' => 'order',
                'channels' => $json([
                    'websocket' => [
                        'enabled' => true,
                        'route' => '/seller/trade/order?order_no={order_no}',
                    ],
                    'wechat' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                    'sms' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                ]),
            ],
            [
                'name' => '订单已发货',
                'title' => '订单已发货',
                'content' => '您的订单#{order_no}已发货，请确认。',
                'type' => 'order_shipped',
                'category' => 'order',
                'channels' => $json([
                    'websocket' => [
                        'enabled' => true,
                        'route' => '/system/order/list?order_no={order_no}',
                    ],
                    'wechat' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                    'sms' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                ]),
            ],
            [
                'name' => '订单已完成',
                'title' => '订单已完成',
                'content' => '您的订单#{order_no}已送达并完成，请确认。',
                'type' => 'order_completed',
                'category' => 'order',
                'channels' => $json([
                    'websocket' => [
                        'enabled' => true,
                        'route' => '/system/order/list?order_no={order_no}',
                    ],
                    'wechat' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                    'sms' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                ]),
            ],
            [
                'name' => '客服任务提醒',
                'title' => '新的客服任务 {task_no}',
                'content' => '您收到新的客服任务【{task_type}】，任务号 {task_no}，请在 {deadline} 前处理。',
                'type' => 'support_task_assigned',
                'category' => 'support',
                'channels' => $json([
                    'websocket' => [
                        'enabled' => true,
                        'route' => '/system/support/tasks?task_no={task_no}',
                    ],
                    'wechat' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                    'sms' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                ]),
            ],
            [
                'name' => '待审核企业提醒',
                'title' => '有 {count} 个企业待审核',
                'content' => '{count} 个企业等待审核，请及时处理。',
                'type' => 'org_audit_pending',
                'category' => 'system',
                'channels' => $json([
                    'websocket' => [
                        'enabled' => true,
                        'route' => '/system/user/org-audit',
                    ],
                    'wechat' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                    'sms' => [
                        'enabled' => true,
                        'route' => '',
                    ],
                ]),
            ],
        ];
    }
}
